package org.solrcn.segment;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.ngram.NGramTokenizer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;

public class ngramDict {

	static DataModel dataModel = new DataModel();

	static List<String>[] startOffsetIndex,endOffsetIndex;

	public static void main(String[] args) throws IOException {
		ngramSplite("中华人民共和国");
	}

	public static void ngramSplite(String text) throws IOException {
		int minGram = 1;
		int maxGram = 3;
		// 1+2+3+...+n=n(n+1)/2
		
		int length = text.length();
		int x = (length-minGram+1)*(length-minGram)/2;
		int y = ((length-maxGram)*(length-maxGram+1))/2;
		int termCount = x-y;
		startOffsetIndex = new ArrayList[termCount];
		endOffsetIndex = new ArrayList[termCount];
		for (int i = 0; i < termCount; i++) {
			startOffsetIndex[i] = new ArrayList<String>();
			endOffsetIndex[i] = new ArrayList<String>();
		}

		NGramTokenizer nGramtoken = new NGramTokenizer(new StringReader(text), minGram, maxGram);
		CharTermAttribute termAtt = nGramtoken.addAttribute(CharTermAttribute.class);
		OffsetAttribute offsetAtt = nGramtoken.addAttribute(OffsetAttribute.class);
		int count = 0;
		while (nGramtoken.incrementToken()) {
//			startOffsetIndex[offsetAtt.startOffset()].add(termAtt.toString());
//			endOffsetIndex[offsetAtt.endOffset()].add(termAtt.toString());
			dataModel.addTerm(termAtt.toString());
			System.out.println(termAtt.toString() + "........" + offsetAtt.startOffset() + "," + offsetAtt.endOffset());
			count++;
		}
		System.out.println(count);
		nGramtoken.close();
		
		for (int i = 0; i < termCount; i++) {
			if(startOffsetIndex[i]!=null && endOffsetIndex[i]!=null){
				System.out.println("====================="+i+"==================");
				for (int j = 0; j < startOffsetIndex[i].size(); j++) {
					System.out.println(startOffsetIndex[i].get(j));
				}
			}
		}
		
	}
}
